package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsStorage;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/brand")
public class BrandController {
@Autowired
private BrandService brandService;
@Autowired
private AdminService adminService;
@Autowired
private CategoryService categoryService;


    @GetMapping("/catAndAdmin")
    public Object catAndAdmin(){
        Map<String,Object> data = new HashMap<>();

        data.put("adminList",adminService.adminList());

        data.put("categoryList",categoryService.categoryList());


        return ResponseUtil.ok(data);


    }


    @PostMapping("/create")
    public Object add(@RequestBody DtsBrand brand)  {

        DtsBrand dtsBrand = brandService.add(brand);
        return ResponseUtil.ok(dtsBrand);
    }
    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand brand){

        DtsBrand dtsBrand = brandService.update(brand);

        return ResponseUtil.ok(dtsBrand);
    }



    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand) {

        brandService.deleted(brand);
        return ResponseUtil.ok();
    }





    @GetMapping("/list")
    public Object list(
            Integer id, String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20")Integer limit,
            @RequestParam(defaultValue = "add_time")String sort,
            @RequestParam(defaultValue = "desc")String order
    ){
        //1. 查询并返回分页对象
        IPage<DtsBrand> resultPage = brandService.queryBrandPage(id,name,page,limit,sort,order);
        //获取结果集
        List<DtsBrand> list = resultPage.getRecords();
        //获取查询总条数
        long total = resultPage.getTotal();
        //封装对象返回
        Map<String,Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",list);
        return ResponseUtil.ok(data);


    }


}
